package neo4j学习;

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

import java.io.File;

public class 嵌入开发 {
    public static void main(String[] args){
//       function1();
       function2();
    }

    private static void function2() {
        /**
         * 读取函数
         */
        GraphDatabaseFactory graphDatabaseFactory = new GraphDatabaseFactory();//获取数据库链接
        GraphDatabaseService graphDatabaseService = graphDatabaseFactory.
                newEmbeddedDatabase(new File("C:/NEO4J"));//建立存储文件
        Transaction transaction = graphDatabaseService.beginTx();//声明数据库事务
        ResourceIterator<Node> nodes = graphDatabaseService.findNodes(LabelTest.Label1);
        nodes.forEachRemaining((test)->{
            System.out.println(test.getId()+","+test.getProperties("name"));
        });
//        nodes.forEachRemaining((asd)->{
//            System.out.println(asd.getLabels()+"2333");
//        });
        transaction.success();//提交事务
    }

    private static void function1() {
        GraphDatabaseFactory graphDatabaseFactory = new GraphDatabaseFactory();//获取数据库链接
        GraphDatabaseService graphDatabaseService = graphDatabaseFactory.
                newEmbeddedDatabase(new File("C:/NEO4J"));//建立存储文件

        try ( Transaction transaction = graphDatabaseService.beginTx()/*声明数据库事务*/){
            Node FirstNode = graphDatabaseService.createNode();//这是数据库自带的NODE节点
            FirstNode.addLabel(LabelTest.Label1);//这里需要一个枚举
            FirstNode.setProperty("属性key","属性value");//添加属性值
            FirstNode.setProperty("name","节点一");
            Node SecondNode = graphDatabaseService.createNode();
            SecondNode.addLabel(LabelTest.Label2);
            SecondNode.setProperty("nodenumber",2);
            FirstNode.createRelationshipTo(SecondNode,RelationTest.Rel2);//添加关系，方向由节点表名
            Node ThreedNode = graphDatabaseService.createNode(LabelTest.Label1);
            ThreedNode.setProperty("name","节点三");
            System.out.println("OK");
            transaction.success();//提交事务
        }


    }

    private enum LabelTest implements Label/*继承自预定义的类*/ {//标签枚举
        Label1,
        Label2,
        Label3,
    }
    private enum RelationTest implements RelationshipType /*同理 关系也要继承*/ {//枚举关系
        Rel1,
        Rel2
    }
}
